#include "utf8_m.h"

static const unsigned char multibyte_length_table[16] = {
	1, 1, 1, 1,	 /* 0x00-0x3F */
	1, 1, 1, 1,	 /* 0x40-0x7F */
	1, 1, 1, 1,	 /* 0x80-0xBF */
	2, 2, 3, 4,	 /* 0xC0-0xFF */
};

unsigned
utf8_octet_count(char oct)
{
	return multibyte_length_table[((unsigned char)oct) >> 4];
}

unsigned
utf8_symcount(const char *str)
{
	unsigned len = 0;
	for (; *str; ++ len)
		str += utf8_octet_count(*str);
	return len;
}

int
utf8_next_octet(char **target)
{
	unsigned count = utf8_octet_count(**target);
	*target += count;
	return 0;
}

const char *
utf8_goto_index(const char *target, unsigned index)
{
	unsigned i = 0;
	const char *ptr;
	for (ptr = target; *ptr; ++ i) {
		if (i == index)
			return ptr;
		ptr += utf8_octet_count(*ptr);
	}
	return 0x0;
}

